﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Web;

    internal class DownloadFileCommandHandler : CommandHandlerBase
    {
        public override void SendResponse(HttpResponse response)
        {
            base.CheckConnector();
            response.ClearHeaders();
            response.Clear();
            response.ContentType = "application/octet-stream";
            try
            {
                base.CheckRequest();
            }
            catch (ConnectorException exception)
            {
                response.AddHeader("X-CKFinder-Error", exception.Number.ToString());
                if (exception.Number == 0x74)
                {
                    response.StatusCode = 0x194;
                }
                else
                {
                    response.StatusCode = 0x193;
                }
                response.End();
                return;
            }
            catch
            {
                response.AddHeader("X-CKFinder-Error", 110.ToString());
                response.StatusCode = 0x193;
                response.End();
                return;
            }
            if (base.CurrentFolder.CheckAcl(AccessControlRules.FileView))
            {
                string str = HttpContext.Current.Request["FileName"];
                string path = Path.Combine(base.CurrentFolder.ServerPath, str);
                if (!base.CurrentFolder.ResourceTypeInfo.CheckExtension(Path.GetExtension(str)))
                {
                    response.StatusCode = 0x193;
                    response.End();
                }
                else if (!File.Exists(path) || Config.Current.CheckIsHiddenFile(str))
                {
                    response.AddHeader("Content-Disposition", "attachment; filename=\"File not found - Please refresh and try again\"");
                    response.StatusCode = 0x194;
                    response.End();
                }
                else
                {
                    response.AddHeader("Content-Disposition", "attachment; filename=\"" + str.Replace("\"", "\\\"") + "\"");
                    byte[] buffer = new byte[0x2710];
                    Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                    long length = stream.Length;
                    while (length > 0L)
                    {
                        if (response.IsClientConnected)
                        {
                            int count = stream.Read(buffer, 0, 0x2710);
                            response.OutputStream.Write(buffer, 0, count);
                            response.Flush();
                            buffer = new byte[0x2710];
                            length -= count;
                        }
                        else
                        {
                            length = -1L;
                        }
                    }
                    response.End();
                }
            }
            else
            {
                response.StatusCode = 0x193;
                response.End();
            }
        }
    }
}

